home *** CD-ROM | disk | FTP | other *** search
/ Total Web Page (Professional Suite) / Total Web Page 99.iso / Javascripts / messages / led.txt < prev    next >
Text File  |  1998-10-30  |  15KB  |  549 lines

  1. <SCRIPT LANGUAGE="JavaScript">
  2.  
  3.  
  4. <!--
  5.  
  6. // set messages (specify backslash in double form (i.e, \\)
  7. var messages = new Array()
  8. messages[0] = "welcome to my page"
  9. messages[1] = "cool led sign eh"
  10. messages[2] = "you can add a lot.."
  11. messages[3] = "of messages to this."
  12.  
  13.  
  14. // number of milliseconds to pause between two messages
  15. var pause = 3000
  16.  
  17. // set normal spacing between two characters (no whitespace inbetween)
  18. var space = 1
  19.  
  20. // set height width of each character
  21. var height = 5
  22. var width = 3
  23.  
  24. // create object of all supported characters in font
  25. var letters = new letterArray()
  26.  
  27. // initialize image variables
  28. var on = new Image(5, 5)
  29. var off = new Image(5, 5)
  30.  
  31. // set image URLs
  32. on.src = "on.gif"
  33. off.src = "off.gif"
  34.  
  35. // get number of images already laid out in page
  36. var imageNum = document.images.length
  37.  
  38. // compute width of board
  39. //var boardWidth = longest * (width + space) - space
  40.  
  41. // set maximum message length in images
  42. var boardWidth = 0
  43. for (var i = 0; i < messages.length; ++i) {
  44.         var lengthWithNoSpaces = messages[i].split(" ").join("").length
  45.         var numberOfSpaces = messages[i].length - lengthWithNoSpaces
  46.         var currentBoardWidth = lengthWithNoSpaces * (width + space) - space + numberOfSpaces * space * 2
  47.         if (boardWidth < currentBoardWidth)
  48.                 boardWidth = currentBoardWidth
  49. }
  50.  
  51. // sign is currently not running
  52. var running = false
  53. var timerID = null
  54.  
  55. function letterArray() {
  56.         this.a = new Array(height)
  57.         this.a[0] = " * "
  58.         this.a[1] = "* *"
  59.         this.a[2] = "***"
  60.         this.a[3] = "* *"
  61.         this.a[4] = "* *"
  62.  
  63.         this.b = new Array(height)
  64.         this.b[0] = "** "
  65.         this.b[1] = "* *"
  66.         this.b[2] = "** "
  67.         this.b[3] = "* *"
  68.         this.b[4] = "** "
  69.  
  70.         this.c = new Array(height)
  71.         this.c[0] = "***"
  72.         this.c[1] = "*  "
  73.         this.c[2] = "*  "
  74.         this.c[3] = "*  "
  75.         this.c[4] = "***"
  76.  
  77.         this.d = new Array(height)
  78.         this.d[0] = "** "
  79.         this.d[1] = "* *"
  80.         this.d[2] = "* *"
  81.         this.d[3] = "* *"
  82.         this.d[4] = "** "
  83.  
  84.         this.e = new Array(height)
  85.         this.e[0] = "*** "
  86.         this.e[1] = "*  "
  87.         this.e[2] = "***"
  88.         this.e[3] = "*  "
  89.         this.e[4] = "***"
  90.  
  91.         this.f = new Array(height)
  92.         this.f[0] = "***"
  93.         this.f[1] = "*  "
  94.         this.f[2] = "***"
  95.         this.f[3] = "*  "
  96.         this.f[4] = "*  "
  97.  
  98.         this.g = new Array(height)
  99.         this.g[0] = "***"
  100.         this.g[1] = "*  "
  101.         this.g[2] = "***"
  102.         this.g[3] = "* *"
  103.         this.g[4] = "***"
  104.  
  105.         this.h = new Array(height)
  106.         this.h[0] = "* *"
  107.         this.h[1] = "* *"
  108.         this.h[2] = "***"
  109.         this.h[3] = "* *"
  110.         this.h[4] = "* *"
  111.  
  112.         this.i = new Array(height)
  113.         this.i[0] = "***"
  114.         this.i[1] = " * "
  115.         this.i[2] = " * "
  116.         this.i[3] = " * "
  117.         this.i[4] = "***"
  118.  
  119.         this.j = new Array(height)
  120.         this.j[0] = "  *"
  121.         this.j[1] = "  *"
  122.         this.j[2] = "  *"
  123.         this.j[3] = "* *"
  124.         this.j[4] = "***"
  125.         
  126.         this.k = new Array(height)
  127.         this.k[0] = "* *"
  128.         this.k[1] = "* *"
  129.         this.k[2] = "** "
  130.         this.k[3] = "* *"
  131.         this.k[4] = "* *"
  132.  
  133.         this.l = new Array(height)
  134.         this.l[0] = "*  "
  135.         this.l[1] = "*  "
  136.         this.l[2] = "*  "
  137.         this.l[3] = "*  "
  138.         this.l[4] = "***"
  139.  
  140.         this.m = new Array(height)
  141.         this.m[0] = "* *"
  142.         this.m[1] = "***"
  143.         this.m[2] = "***"
  144.         this.m[3] = "* *"
  145.         this.m[4] = "* *"
  146.  
  147.         this.n = new Array(height)
  148.         this.n[0] = "* *"
  149.         this.n[1] = "***"
  150.         this.n[2] = "***"
  151.         this.n[3] = "***"
  152.         this.n[4] = "* *"
  153.  
  154.         this.o = new Array(height)
  155.         this.o[0] = "***"
  156.         this.o[1] = "* *"
  157.         this.o[2] = "* *"
  158.         this.o[3] = "* *"
  159.         this.o[4] = "***"
  160.  
  161.         this.p = new Array(height)
  162.         this.p[0] = "** "
  163.         this.p[1] = "* *"
  164.         this.p[2] = "** "
  165.         this.p[3] = "*  "
  166.         this.p[4] = "*  "
  167.  
  168.         this.q = new Array(height)
  169.         this.q[0] = "***"
  170.         this.q[1] = "* *"
  171.         this.q[2] = "* *"
  172.         this.q[3] = "***"
  173.         this.q[4] = "***"
  174.  
  175.         this.r = new Array(height)
  176.         this.r[0] = "** "
  177.         this.r[1] = "* *"
  178.         this.r[2] = "** "
  179.         this.r[3] = "* *"
  180.         this.r[4] = "* *"
  181.  
  182.         this.s = new Array(height)
  183.         this.s[0] = "***"
  184.         this.s[1] = "*  "
  185.         this.s[2] = "***"
  186.         this.s[3] = "  *"
  187.         this.s[4] = "***"
  188.  
  189.         this.t = new Array(height)
  190.         this.t[0] = "***"
  191.         this.t[1] = " * "
  192.         this.t[2] = " * "
  193.         this.t[3] = " * "
  194.         this.t[4] = " * "
  195.  
  196.         this.u = new Array(height)
  197.         this.u[0] = "* *"
  198.         this.u[1] = "* *"
  199.         this.u[2] = "* *"
  200.         this.u[3] = "* *"
  201.         this.u[4] = "***"
  202.  
  203.         this.v = new Array(height)
  204.         this.v[0] = "* *"
  205.         this.v[1] = "* *"
  206.         this.v[2] = "* *"
  207.         this.v[3] = "* *"
  208.         this.v[4] = " * "
  209.  
  210.         this.w = new Array(height)
  211.         this.w[0] = "* *"
  212.         this.w[1] = "* *"
  213.         this.w[2] = "***"
  214.         this.w[3] = "***"
  215.         this.w[4] = "***"
  216.  
  217.         this.x = new Array(height)
  218.         this.x[0] = "* *"
  219.         this.x[1] = "* *"
  220.         this.x[2] = " * "
  221.         this.x[3] = "* *"
  222.         this.x[4] = "* *"
  223.  
  224.         this.y = new Array(height)
  225.         this.y[0] = "* *"
  226.         this.y[1] = "* *"
  227.         this.y[2] = "***"
  228.         this.y[3] = " * "
  229.         this.y[4] = " * "
  230.  
  231.         this.z = new Array(height)
  232.         this.z[0] = "***"
  233.         this.z[1] = "  *"
  234.         this.z[2] = " * "
  235.         this.z[3] = "*  "
  236.         this.z[4] = "***"
  237.  
  238.         this['!'] = new Array(height)
  239.         this['!'][0] = " * "
  240.         this['!'][1] = " * "
  241.         this['!'][2] = " * "
  242.         this['!'][3] = "   "
  243.         this['!'][4] = " * "
  244.  
  245.         this[':'] = new Array(height)
  246.         this[':'][0] = "   "
  247.         this[':'][1] = " * "
  248.         this[':'][2] = "   "
  249.         this[':'][3] = " * "
  250.         this[':'][4] = "   "
  251.  
  252.         this['.'] = new Array(height)
  253.         this['.'][0] = "   "
  254.         this['.'][1] = "   "
  255.         this['.'][2] = "   "
  256.         this['.'][3] = "   "
  257.         this['.'][4] = " * "
  258.  
  259.         this['='] = new Array(height)
  260.         this['='][0] = "   "
  261.         this['='][1] = "***"
  262.         this['='][2] = "   "
  263.         this['='][3] = "***"
  264.         this['='][4] = "   "
  265.  
  266.         this['='] = new Array(height)
  267.         this['='][0] = "   "
  268.         this['='][1] = "***"
  269.         this['='][2] = "   "
  270.         this['='][3] = "***"
  271.         this['='][4] = "   "
  272.  
  273.         this['+'] = new Array(height)
  274.         this['+'][0] = "   "
  275.         this['+'][1] = " * "
  276.         this['+'][2] = "***"
  277.         this['+'][3] = " * "
  278.         this['+'][4] = "   "
  279.  
  280.         this['-'] = new Array(height)
  281.         this['-'][0] = "   "
  282.         this['-'][1] = "   "
  283.         this['-'][2] = "***"
  284.         this['-'][3] = "   "
  285.         this['-'][4] = "   "
  286.  
  287.         this['/'] = new Array(height)
  288.         this['/'][0] = "  *"
  289.         this['/'][1] = "  *"
  290.         this['/'][2] = " * "
  291.         this['/'][3] = "*  "
  292.         this['/'][4] = "*  "
  293.  
  294.         this['\\'] = new Array(height)
  295.         this['\\'][0] = "*  "
  296.         this['\\'][1] = "*  "
  297.         this['\\'][2] = " * "
  298.         this['\\'][3] = "  *"
  299.         this['\\'][4] = "  *"
  300.  
  301.         this['\\'] = new Array(height)
  302.         this['\\'][0] = "*  "
  303.         this['\\'][1] = "*  "
  304.         this['\\'][2] = " * "
  305.         this['\\'][3] = "  *"
  306.         this['\\'][4] = "  *"
  307.  
  308.         this['"'] = new Array(height)
  309.         this['"'][0] = "* *"
  310.         this['"'][1] = "* *"
  311.         this['"'][2] = "* *"
  312.         this['"'][3] = "   "
  313.         this['"'][4] = "   "
  314.  
  315.         this["'"] = new Array(height)
  316.         this["'"][0] = " * "
  317.         this["'"][1] = " * "
  318.         this["'"][2] = " * "
  319.         this["'"][3] = "   "
  320.         this["'"][4] = "   "
  321.  
  322.         this['('] = new Array(height)
  323.         this['('][0] = "  *"
  324.         this['('][1] = " * "
  325.         this['('][2] = " * "
  326.         this['('][3] = " * "
  327.         this['('][4] = "  *"
  328.  
  329.         this[')'] = new Array(height)
  330.         this[')'][0] = "*  "
  331.         this[')'][1] = " * "
  332.         this[')'][2] = " * "
  333.         this[')'][3] = " * "
  334.         this[')'][4] = "*  "
  335.  
  336.         this['*'] = new Array(height)
  337.         this['*'][0] = "   "
  338.         this['*'][1] = "***"
  339.         this['*'][2] = "***"
  340.         this['*'][3] = "***"
  341.         this['*'][4] = "   "
  342.  
  343.         this['?'] = new Array(height)
  344.         this['?'][0] = "** "
  345.         this['?'][1] = "  *"
  346.         this['?'][2] = " * "
  347.         this['?'][3] = "   "
  348.         this['?'][4] = " * "
  349.  
  350.         this['0'] = new Array(height)
  351.         this['0'][0] = " * "
  352.         this['0'][1] = "* *"
  353.         this['0'][2] = "* *"
  354.         this['0'][3] = "* *"
  355.         this['0'][4] = " * "
  356.  
  357.         this['1'] = new Array(height)
  358.         this['1'][0] = " * "
  359.         this['1'][1] = " * "
  360.         this['1'][2] = " * "
  361.         this['1'][3] = " * "
  362.         this['1'][4] = " * "
  363.  
  364.         this['2'] = new Array(height)
  365.         this['2'][0] = "***"
  366.         this['2'][1] = "  *"
  367.         this['2'][2] = "***"
  368.         this['2'][3] = "*  "
  369.         this['2'][4] = "***"
  370.  
  371.         this['3'] = new Array(height)
  372.         this['3'][0] = "***"
  373.         this['3'][1] = "  *"
  374.         this['3'][2] = "***"
  375.         this['3'][3] = "  *"
  376.         this['3'][4] = "***"
  377.  
  378.         this['4'] = new Array(height)
  379.         this['4'][0] = "* *"
  380.         this['4'][1] = "* *"
  381.         this['4'][2] = "***"
  382.         this['4'][3] = "  *"
  383.         this['4'][4] = "  *"
  384.  
  385.         this['5'] = new Array(height)
  386.         this['5'][0] = "***"
  387.         this['5'][1] = "*  "
  388.         this['5'][2] = "***"
  389.         this['5'][3] = "  *"
  390.         this['5'][4] = "** "
  391.  
  392.         this['6'] = new Array(height)
  393.         this['6'][0] = "** "
  394.         this['6'][1] = "*  "
  395.         this['6'][2] = "***"
  396.         this['6'][3] = "* *"
  397.         this['6'][4] = "***"
  398.  
  399.         this['7'] = new Array(height)
  400.         this['7'][0] = "***"
  401.         this['7'][1] = "  *"
  402.         this['7'][2] = " * "
  403.         this['7'][3] = "*  "
  404.         this['7'][4] = "*  "
  405.  
  406.         this['8'] = new Array(height)
  407.         this['8'][0] = "***"
  408.         this['8'][1] = "* *"
  409.         this['8'][2] = "***"
  410.         this['8'][3] = "* *"
  411.         this['8'][4] = "***"
  412.  
  413.         this['9'] = new Array(height)
  414.         this['9'][0] = "***"
  415.         this['9'][1] = "* *"
  416.         this['9'][2] = "***"
  417.         this['9'][3] = "  *"
  418.         this['9'][4] = "***"
  419. }
  420.  
  421. function drawBlank() {
  422.         // assign greater than symbol to variable
  423.         var gt = unescape("%3e")
  424.  
  425.         document.write('<TABLE BORDER=2 CELLPADDING=8' + gt + '<TR' + gt + '<TD BGCOLOR ALIGN="center" VALIGN="center"' + gt)
  426.  
  427.         // print entire board of off images
  428.         for (var y = 0; y < height; ++y) {
  429.                 for (var x = 0; x < boardWidth; ++x) {
  430.                         document.write('<IMG SRC="' + off.src + '" HEIGHT=5 WIDTH=5' + gt)
  431.                 }
  432.                 document.write('<BR' + gt)
  433.         }
  434.         document.write('</TD' + gt + '</TR' + gt + '</TABLE' + gt)
  435. }
  436.  
  437. function setLight(state, x, y) {
  438.         // set a specific light in sign to on (true) or off (false)
  439.         if (state)
  440.                 document.images[computeIndex(x, y)].src = on.src
  441.         else
  442.                 document.images[computeIndex(x, y)].src = off.src
  443. }
  444.  
  445. function drawLetter(letter, startX) {
  446.         // draws a letter at the given x coordinate
  447.         for (var x = 0; x < width; ++x) {
  448.                 for (var y = 0; y < height; ++y) {
  449.                         setLight(letters[letter][y].charAt(x) == "*", startX + x, y)
  450.                 }
  451.         }
  452. }
  453.  
  454. function drawSpace(startX) {
  455.         // create a small space between each two characters
  456.         for (var x = 0; x < space; ++x) {
  457.                 for (var y = 0; y < height; ++y) {
  458.                         setLight(false, startX + x, y)
  459.                 }
  460.         }
  461. }
  462.  
  463. function computeIndex(x, y) {
  464.         // compute the document index of an image in the sign, based on the x-y coordinates 
  465.         return (y * boardWidth + x) + imageNum
  466. }
  467.  
  468. function floodBoard(startX) {
  469.         // set all lights from startX to off
  470.         for (var x = startX; x < boardWidth; ++x) {
  471.                 for (var y = 0; y < height; ++y) {
  472.                         setLight(false, x, y)
  473.                 }
  474.         }
  475. }
  476.  
  477. function drawMessage(num) {
  478.         // initialize variable to current message
  479.         var text = messages[num]
  480.  
  481.         // initialize two counters (j - current character in message, i - current x coordinate)
  482.         var i = 0
  483.         var j = 0
  484.  
  485.         while (1) {
  486.                 if (text.charAt(j) != " ") {
  487.                         // draw current letter
  488.                         drawLetter(text.charAt(j), i)
  489.         
  490.                         // increment i by the constant width of an image
  491.                         i += width
  492.                 } else {
  493.                         // add an extra space (do not advance j yet)
  494.                         drawSpace(i)
  495.                         i += space
  496.                 }
  497.  
  498.                 // if j is less that index of last character
  499.                 if (j < text.length - 1) {
  500.                         drawSpace(i)
  501.                         i += space
  502.                 } else // j is the index of the last character (lsat character already printed)
  503.                         break
  504.  
  505.                 // increment j by one because one letter was printed
  506.                 ++j
  507.         }
  508.  
  509.         // flood the remaining piece of the sign (turn it off)
  510.         floodBoard(i)
  511.  
  512.         // if message printed this time was not the last one in the array
  513.         if (num < messages.length - 1)
  514.                 // val *must* be a global variable for use with the timeout
  515.                 val = ++num
  516.         else
  517.                 val = 0 // start cycle over again
  518.         
  519.         // recursive call after waiting 3 seconds (some of the time already passed during printing)
  520.         timerID = setTimeout("drawMessage(val)", pause)
  521. }
  522.  
  523. function startSign() {
  524.         running = true
  525.  
  526.         // wait 3 seconds and then call function to print first message
  527.         drawMessage(0)
  528. }
  529.  
  530. function stopSign() {
  531.         if(running)
  532.                 clearTimeout(timerID)
  533.         running = false
  534. }
  535.  
  536. // open form
  537. document.write('<FORM>')
  538.  
  539. // create initial sign (all sign is off)
  540. drawBlank()
  541.  
  542. document.write('<INPUT TYPE="button" VALUE="start" onClick="startSign()">')
  543. document.write('<INPUT TYPE="button" VALUE="stop" onClick="stopSign(); floodBoard(0)">')
  544. document.write('</FORM>')
  545.  
  546. // -->
  547.  
  548.  
  549. </SCRIPT>